/**
 * @typedef {import('three/examples/jsm/controls/TransformControls.js').TransformControls} TransformControls
 */

import * as THREE from 'three';
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';

import { initMxintBloomPassesComposer_thirdparty } from './bloom';
import { initoutlinePassesAndComposer } from './outline';

export const ENTIRE_SCENE = 0;
export const BLOOM_SCENE = 2;
export const OUTLINE_SCENE = 1;

/**
 * TODO: 将bloom和outline中composer进行合并
 *  FIXME:暂时有问题,考虑编辑器上用outline，大屏上用bloom
 * @deprecated 通道有问题
 * @param {object} option
 * @param {string} option.containerId
 * @param {import('three').Scene} option.scene
 * @param {import('three').PerspectiveCamera} option.camera
 * @param {import('three').WebGLRenderer} option.renderer
 * @param {TransformControls} option.transformControls
 * @param {array} option.objects
 */
export function initFinalComposer(option) {
  const { renderer, camera, scene } = option;

  const { finalComposer: bloomComposer } = initMxintBloomPassesComposer_thirdparty(option);

  const { outlineComposer } = initoutlinePassesAndComposer(option);

  const fragmentShader = `
uniform sampler2D baseTexture;
uniform sampler2D bloomTexture;
uniform sampler2D outlineTexture;
uniform vec3 glowColor;
varying vec2 vUv;

void main() {
  vec4 base_color = texture2D(baseTexture, vUv);
  vec4 bloom_color = texture2D(bloomTexture, vUv);
  vec4 outline_color = texture2D(outlineTexture, vUv);
  
  // 计算辉光亮度
  float bloom_lum = 0.21 * bloom_color.r + 0.71 * bloom_color.g + 0.07 * bloom_color.b;
  
  // 计算轮廓亮度（可以根据需要调整轮廓检测算法）
  float outline_lum = max(outline_color.r, max(outline_color.g, outline_color.b));
  
  // 混合基础颜色和辉光效果
  vec3 combined_color = base_color.rgb + bloom_color.rgb;
  
  // 添加轮廓效果（使用自定义颜色）
  combined_color = mix(combined_color, glowColor, outline_lum * outline_color.a);
  
  // 最终输出
  gl_FragColor = vec4(combined_color, max(base_color.a, max(bloom_lum, outline_lum)));
}
`;
  const vertexShader = `
varying vec2 vUv;
void main() {
  vUv = uv;
  gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
} 
`;
  const finalPass = new ShaderPass(
    new THREE.ShaderMaterial({
      uniforms: {
        baseTexture: { value: null },
        // finalPass读取了bloomComposer生成的texture（EffectComposer默认生成两张texture，一张用来读，一张用来写，renderTarget2.texture对应写入的那张）
        bloomTexture: { value: bloomComposer.renderTarget2.texture },
        outlineTexture: { value: outlineComposer.renderTarget2.texture },
        tDiffuse: { value: null },
        glowColor: { value: null },
      },
      vertexShader: vertexShader,
      fragmentShader: fragmentShader,
      defines: {},
    }),
    'baseTexture'
  );
  finalPass.needsSwap = true;
  finalPass.material.uniforms.glowColor.value = new THREE.Color();
  const finalComposer = new EffectComposer(renderer);

  const finalRenderPass = new RenderPass(scene, camera);
  finalComposer.addPass(finalRenderPass);

  // 添加合成通道
  finalComposer.addPass(finalPass);

  // FIXME: 有问题
  const renderFinalComposer = () => {
    camera.layers.set(BLOOM_SCENE);
    bloomComposer.render();
    camera.layers.set(OUTLINE_SCENE);
    outlineComposer.render();
    camera.layers.set(ENTIRE_SCENE);
    finalComposer.render();
  };
  return {
    renderFinalComposer,
    finalComposer,
    outlineComposer,
  };
}
